﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/wwwroot/bootstrap.min.css" />
    <link rel="stylesheet" href="/wwwroot/common/css/sapar.css?v=" />
    <link rel="stylesheet" href="/wwwroot/common/css/index_inner.css?v=" />
   
    <script type="text/javascript" src="/wwwroot/config/appsettings.js"></script>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="X-UA-Compatible" content="IE=8">

    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <title>Main</title>

    <style>
        
    </style>
</head>
<body >
    <div id="container" class="d-flex vh-100">
        <div id="bd" class="d-flex flex-grow-1">
            <div class="wrap d-flex flex-grow-1">
                <div class="sidebar bg-light" style="width: 250px;">
                    <h2 class="sidebar-header p-3 bg-primary text-white">
                        <p id="menuHeader" class="m-0"></p>
                    </h2>
                    <ul class="nav flex-column">
                        <!-- Menu items will be populated here -->
                    </ul>
                </div>
                <div class="content flex-grow-1 position-relative">
                    <div id="loading" class="position-absolute w-100 h-100 d-flex justify-content-center align-items-center" style="background: rgba(255,255,255,0.8); z-index: 1000; display: none;">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                    </div>
                    <iframe src="" id="iframe" name="iframe" ></iframe>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function RefreshIframe() {
            setTimeout("Refreshfun()", 1000)
        }
        function Refreshfun() {
            window.frames["iframe"].document.getElementById("hid_pageIndex").value = window.frames["iframe"].document.getElementById("hid_pageIndex_temp").value;
            window.frames["iframe"].document.getElementById('but_Search').click();
        }
    </script>

    <script type="text/javascript" src="/wwwroot/common/js/jquery.js"></script>
    <script type="text/javascript" src="/wwwroot/common/js/sapar.js"></script>
    <script type="text/javascript" src="/wwwroot/common/js/index_inner.js"></script>

    <script type="text/javascript">
        function getQueryParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURIComponent(r[2]);
            return null;
        }

        $(document).ready(function() {
            // 获取并显示menuguid
            var menuGuid = getQueryParam('menuguid');
            var name = getQueryParam('name');

            const randomCode = Date.now();
            if (name) {
                $('#menuHeader').text(name);
            }
            
            // Show loading when iframe src changes
            $('#iframe').on('loadstart', function() {
                $('#loading').show();
            });
$.ajax({
    url:  product_file_server_api_host+'/api/Menu/getTopMenu?parentGuid='+menuGuid+ '&randomCode=' + randomCode,
    type: 'POST',
    headers: {
        'Authorization':  localStorage.getItem('token')
    },
    success: function(response) {
                    var $menu = $('ul.nav');
                    $menu.empty();
                    
                    if (response && response.data && Array.isArray(response.data)) {
                        // 显示菜单guid
                       
                        
                        response.data.forEach(function(item) {
                            var $li = $('<li>').addClass(item.Menu_IconCss || '');
                            var $header = $('<div>').addClass('nav-header');
                            var $link = $('<a>')
                                .attr('href', 'javascript:;')
                                .attr('data-src', item.menuLinkUrl || '')
                                .addClass('clearfix')
                                .append($('<span>').text(item.menuName))
                                .append($('<i>').addClass('icon'));
                            
                            $header.append($link);
                            $li.append($header);
                            
                            // 如果有子菜单
                            if (item.children && item.children.length > 0) {
                                var $subnav = $('<ul>').addClass('subnav');
                                item.children.forEach(function(child) {
                                    $subnav.append(
                                        $('<li>').append(
                                            $('<a>')
                                                .attr('href', 'javascript:;')
                                                .attr('data-src', child.menuLinkUrl || '')
                                                .text(child.menuName)
                                        )
                                    );
                                });
                                $li.append($subnav);
                            }
                            
                            $menu.append($li);
                        });
                    } else {
                        console.error('Invalid menu data format:', response);
                    }
                },
                error: function(xhr, status, error) {
                    if (xhr.status === 0) {
                        // CORS error
                        console.error('CORS error: Failed to load menu. Please ensure the server is properly configured to handle cross-origin requests.');
                        alert('Failed to load menu due to CORS restrictions. Please contact support.');
                    } else {
                        console.error('Failed to load menu:', error);
                    }
                }
            });
        });
    </script>
</body>
</html>
